package com.cleaningcloud.cld.service.impl.mini;

import com.cleaningcloud.cld.domain.*;
import com.cleaningcloud.cld.domain.dto.*;
import com.cleaningcloud.cld.domain.vo.*;
import com.cleaningcloud.cld.mapper.*;
import com.cleaningcloud.cld.mapper.mini.MiniProjectMapper;
import com.cleaningcloud.cld.service.ICldComplaintsService;
import com.cleaningcloud.cld.service.ICldProjectPostService;
import com.cleaningcloud.cld.service.mini.IMiniProjectService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class MiniProjectServiceImpl implements IMiniProjectService {

    @Autowired
    private CldStaffMapper cldStaffMapper;

    @Autowired
    private CldWorkplanMapper cldWorkplanMapper;

    @Autowired
    private CldWorkplanDetailsMapper cldWorkplanDetailsMapper;

    @Autowired
    private MiniProjectMapper miniProjectMapper;

    @Autowired
    private CldWorkplanSubmitMapper cldWorkplanSubmitMapper;

    @Autowired
    private ICldComplaintsService complaintsService;

    @Autowired
    private ICldProjectPostService cldProjectPostService;

    @Autowired
    private CldRegularPlanDetailsMapper cldRegularPlanDetailsMapper;

    /**
     * @Author Shaw
     * @Description 小程序(客户端)-查询工作计划详情
     * @date: 2025-06-10 16:38
     * @param: //TODO
     */
    @Override
    public CldWorkplanVo getCustomerWorkPlanInfo(Long workPlanId, String rowDate, Long postId,Long staffId){
        CldWorkplanVo cldWorkplanVo = miniProjectMapper.getWorkPlanById(workPlanId);
        CldStaffVo cldStaffVo = cldStaffMapper.selectCldStaffByStaffId(staffId);
        if(null != cldStaffVo){
            cldWorkplanVo.setStaffName(cldStaffVo.getStaffName());
        }
        CldWorkplanDetailsVo cwd = new CldWorkplanDetailsVo();
        cwd.setWorkplanId(workPlanId);
        cwd.setStaffId(staffId);
        //根据工作计划查询详情
        List<CldWorkplanDetailsVo> cwdList = miniProjectMapper.getWorkPlanDetailsByWorkPlanIdList(cwd);
        if(!cwdList.isEmpty()){
            for(CldWorkplanDetailsVo cwdVo : cwdList){
                CldWorkplanSubmitVo cwsv = new CldWorkplanSubmitVo();
                cwsv.setWorkplanDetailsId(cwdVo.getWorkplanDetailsId());
                cwsv.setRowDate(rowDate);
                cwsv.setStaffId(staffId);
                cwsv.setPostId(postId);
                //根据工作计划详情查询提交明细
                List<CldWorkplanSubmit> cwsList = miniProjectMapper.getWorkPlanSubmitByWorkPlanDetailsId(cwsv);
                if(!cwsList.isEmpty()){
                    cwdVo.setSubmitList(cwsList);
                }
            }
            cldWorkplanVo.setDetailsList(cwdList);
        }
        return cldWorkplanVo;
    }

    @Override
    public CldWorkplanVo selectCldWorkplanByWorkplanId(Long workPlanId, Date rowDate, Long postId) {
        CldWorkplanVo cldWorkplanVo = cldWorkplanMapper.selectCldWorkplanByWorkplanId(workPlanId);
        for (CldWorkplanDetailsVo cldWorkplanDetails : cldWorkplanVo.getDetailsList()) {
            CldWorkplanSubmitDto query = new CldWorkplanSubmitDto();
            query.setWorkplanId(cldWorkplanVo.getWorkplanId());
            query.setWorkplanDetailsId(cldWorkplanDetails.getWorkplanDetailsId());
            query.setProjectPostId(postId);
            query.setRowDate(rowDate);
            // 日清洁 查询当日提交记录
            if (cldWorkplanVo.getCleanNature() == 1) {
                query.setRowDate(rowDate);
            } else if (cldWorkplanVo.getCleanNature() == 2) {
                query.setDateType(cldWorkplanDetails.getDateType());
            }
            List<CldWorkplanSubmit> submitList = cldWorkplanSubmitMapper.selectCldWorkplanSubmitList(query);
            cldWorkplanDetails.setSubmitList(submitList);
        }
        return cldWorkplanVo;
    }

    @Override
    @Transactional
    public int submitWorkPlan(CldWorkplanDto cldWorkplanDto) {
        CldWorkplanVo cldWorkplanVo = cldWorkplanMapper.selectCldWorkplanByWorkplanId(cldWorkplanDto.getWorkplanId());
        if (cldWorkplanVo == null) {
            throw new RuntimeException("工作计划查询失败！");
        }
        if (!cldWorkplanDto.getDetailsList().isEmpty()) {
            for (CldWorkplanDetailsDto details : cldWorkplanDto.getDetailsList()) {
                CldWorkplanDetails cldWorkplanDetails = cldWorkplanDetailsMapper.getCldWorkplanDetailsByWorkplanDetailsId(details.getWorkplanDetailsId());
                // 当前时段提交次数
                CldWorkplanSubmitDto query = new CldWorkplanSubmitDto();
                query.setWorkplanId(cldWorkplanVo.getWorkplanId());
                query.setWorkplanDetailsId(cldWorkplanDetails.getWorkplanDetailsId());
                query.setStaffId(SecurityUtils.getUserId());
                query.setProjectPostId(cldWorkplanDto.getPostId());
                // 如为专项清洁
                if (cldWorkplanVo.getCleanNature() == 2) {
                    query.setDateType(cldWorkplanDetails.getDateType());
                } else {
                    query.setDateType(0L);
                }
                List<CldWorkplanSubmit> submitList = cldWorkplanSubmitMapper.selectCldWorkplanSubmitList(query);
                if (cldWorkplanVo.getCleanNature() == 2) {
                    if (submitList != null && submitList.size() >= cldWorkplanDetails.getFrequency()) {
                        throw new RuntimeException("提交次数大于设定的提交频率次数！");
                    }
                } else {
                    if (submitList != null && !submitList.isEmpty() && submitList.size() >= cldWorkplanVo.getDetailsList().size()) {
                        throw new RuntimeException("当日该时段已提交,请勿重复提交！");
                    }
                }
                CldWorkplanSubmit cldWorkplanSubmit = new CldWorkplanSubmit();
                cldWorkplanSubmit.setSubmitTime(DateUtils.getNowDate());
                cldWorkplanSubmit.setStaffId(SecurityUtils.getUserId());
                cldWorkplanSubmit.setRemark(cldWorkplanDto.getRemark());
                cldWorkplanSubmit.setProjectPostId(cldWorkplanDto.getPostId());
                cldWorkplanSubmit.setApprovalStatus(1L);
                // 提交为日清洁
                if (cldWorkplanDto.getCleanNature() == 1) {
                    cldWorkplanSubmit.setWorkSubmitId(details.getSubmitId());
                    cldWorkplanSubmitMapper.updateCldWorkplanSubmit(cldWorkplanSubmit);
                } else {
                    cldWorkplanSubmit.setRowDate(DateUtils.getNowDate());
                    cldWorkplanSubmit.setWorkplanId(cldWorkplanVo.getWorkplanId());
                    cldWorkplanSubmit.setWorkplanDetailsId(cldWorkplanDetails.getWorkplanDetailsId());
                    cldWorkplanSubmitMapper.insertCldWorkplanSubmit(cldWorkplanSubmit);
                }

            }
        }
        return 1;
    }

    @Override
    public List<CldWorkplan> getWorkPlanList(CldWorkplanDto cldWorkplanDto) {
        List<CldWorkplan> list = new ArrayList<>();
        if (cldWorkplanDto.getCleanNature() == 1) {
            list = miniProjectMapper.getSubmitWorkPlanList(cldWorkplanDto);
        } else {
            // 专项清洁时 查询岗位id
            CldProjectPostSetVo projectPostSetVo = cldProjectPostService.selectCldProjectPostByProjectPostId(cldWorkplanDto.getPostId());
            cldWorkplanDto.setProjectPostId(projectPostSetVo.getProjectPostId());
            cldWorkplanDto.setPostId(projectPostSetVo.getProjectPostName());
            list = miniProjectMapper.getWorkPlanList(cldWorkplanDto);
        }
        return list;
    }

    @Override
    public List<Map<String,Object>> getWorkPlanListDate(CldWorkplanDto cldWorkplanDto) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Map<String,Object>> rowDteList = miniProjectMapper.getWorkPlanRowDateList(cldWorkplanDto);
        if(!rowDteList.isEmpty()) {
            for (Map<String,Object> rowDate : rowDteList) {
                cldWorkplanDto.setRowDate(sdf.format(rowDate.get("rowDate")));
                cldWorkplanDto.setGroupName(null);
                List<Map<String,Object>> groupList = miniProjectMapper.getWorkPlanGroupNameByRowDateList(cldWorkplanDto);
                if(!groupList.isEmpty()) {
                    rowDate.put("groupNameList", groupList);
                    for (Map<String,Object> group : groupList) {
                        cldWorkplanDto.setGroupName(Long.valueOf(group.get("groupName").toString()));
                        List<CldWorkplanVo> workPlanList = miniProjectMapper.getWorkPlanListDate(cldWorkplanDto);
                        if(!workPlanList.isEmpty()) {
                            group.put("workPlanList", workPlanList);
                        }
                    }
                }
            }
            return rowDteList;
        }
        return null;
    }

    /**
     * @Author Shaw
     * @Description 小程序(客户端) - 工作计划审核
     * @date: 2025-10-30 10:14
     * @param: //TODO
     */
    @Override
    @Transactional
    public int auditWorkPlan(CldWorkplanDto cldWorkplanDto) {
        CldWorkplan cldWorkplan = cldWorkplanMapper.selectCldWorkplanByWorkplanId(cldWorkplanDto.getWorkplanId());
        if (cldWorkplan == null) {
            throw new RuntimeException("工作计划查询失败！");
        }
        // 驳回工作计划
        if (cldWorkplanDto.getApprovalStatus() == 4) {
            // 生成一条客诉
            CldComplaintsDto cldComplaintsDto = new CldComplaintsDto();
            cldComplaintsDto.setProjectId(cldWorkplan.getProjectId());
            cldComplaintsDto.setComplaintType(3L);
            cldComplaintsDto.setComplaintDesc(cldWorkplanDto.getApprovalReject());
            complaintsService.insertCldComplaints(cldComplaintsDto);
        }
        if(cldWorkplanDto.getCleanNature() != null && cldWorkplanDto.getCleanNature() == 2){
            for (CldWorkplanDetailsDto cldWorkplanDetailsDto : cldWorkplanDto.getDetailsList()) {
                // 当前时段提交次数
                CldWorkplanSubmitDto query = new CldWorkplanSubmitDto();
                query.setWorkplanId(cldWorkplanDto.getWorkplanId());
                query.setWorkplanDetailsId(cldWorkplanDetailsDto.getWorkplanDetailsId());
                query.setProjectPostId(cldWorkplanDto.getPostId());
                query.setDateType(cldWorkplanDetailsDto.getDateType());
                query.setApprovalStatus(1L);
                query.setRowDate(DateUtils.parseDate(cldWorkplanDto.getRowDate()));
                List<CldWorkplanSubmit> submitList = cldWorkplanSubmitMapper.selectCldWorkplanSubmitList(query);
                for (CldWorkplanSubmit cldWorkplanSubmit : submitList) {
                    cldWorkplanSubmit.setApprovalStatus(cldWorkplanDto.getApprovalStatus());
                    cldWorkplanSubmit.setApprovalId(SecurityUtils.getUserId());
                    cldWorkplanSubmit.setApprovalTime(DateUtils.getNowDate());
                    cldWorkplanSubmit.setApprovalImg(cldWorkplanDto.getApprovalImg());
                    cldWorkplanSubmit.setRemark(cldWorkplanDto.getRemark());
                    cldWorkplanSubmitMapper.updateCldWorkplanSubmit(cldWorkplanSubmit);
                }
            }
        }else{
            if (!cldWorkplanDto.getSubmitIds().isEmpty()) {
                for (Long submitId : cldWorkplanDto.getSubmitIds()) {
                    CldWorkplanSubmit workplanSubmit = new CldWorkplanSubmit();
                    workplanSubmit.setWorkSubmitId(submitId);
                    workplanSubmit.setApprovalStatus(cldWorkplanDto.getApprovalStatus());
                    workplanSubmit.setApprovalId(SecurityUtils.getUserId());
                    workplanSubmit.setApprovalTime(DateUtils.getNowDate());
                    workplanSubmit.setApprovalImg(cldWorkplanDto.getApprovalImg());
                    workplanSubmit.setRemark(cldWorkplanDto.getRemark());
                    cldWorkplanSubmitMapper.updateCldWorkplanSubmit(workplanSubmit);
                }
            } else {
                throw new RuntimeException("当日暂无人员提交工作计划！");
            }
        }
        return 1;
    }

    /**
     * @Author Shaw
     * @Description 小程序(客户端) - 工作(日)计划批量审核
     * @date: 2025-06-10 16:34
     * @param: //TODO
     */
    @Override
    @Transactional
    public int batchAuditWorkPlan(CldWorkplanDto cldWorkplanDto){
        int result = 0;
        if(!cldWorkplanDto.getSubmitList().isEmpty()){
            for (CldWorkplanSubmit submit : cldWorkplanDto.getSubmitList()){
                CldWorkplanSubmitDto query = new CldWorkplanSubmitDto();
                query.setRowDate(submit.getRowDate());
                query.setStaffId(submit.getStaffId());
                query.setProjectPostId(submit.getProjectPostId());
                query.setWorkplanId(submit.getWorkplanId());
                List<CldWorkplanSubmit> submitList = cldWorkplanSubmitMapper.selectCldWorkplanSubmitList(query);
                if(!submitList.isEmpty()){
                    for (CldWorkplanSubmit cldWorkplanSubmit : submitList) {
                        cldWorkplanSubmit.setApprovalStatus(2L);
                        cldWorkplanSubmit.setApprovalTime(DateUtils.getNowDate());
                        cldWorkplanSubmit.setApprovalId(submit.getApprovalId());
                        result = cldWorkplanSubmitMapper.updateCldWorkplanSubmit(cldWorkplanSubmit);
                    }
                }else{
                    throw new RuntimeException("未查询到工作计划提交明细数据");
                }
            }
        }else{
            throw new RuntimeException("未查询到工作计划提交列表参数");
        }
        return result;
    }

    //---------------------------------------  例会相关 ---------------------------------------
    /**
     * @Author Shaw
     * @Description 查询当天和历史例会记录
     * @date: 2025-06-11 12:05
     * @param: //TODO
     */
    @Override
    public List<CldRegularMeetingVo> getRegularMeetingList(CldRegularMeetingDto cldRegularMeetingDto) {
        return miniProjectMapper.getRegularMeetingList(cldRegularMeetingDto);
    }

    /**
     * @Author Shaw
     * @Description 查询例会计划列表记录
     * @date: 2025-06-11 15:06
     * @param: //TODO
     */
    @Override
    public List<CldRegularPlanVo> getRegularPlanList(CldRegularPlanDto cldRegularPlanDto) {
        List<CldRegularPlanVo> list = miniProjectMapper.getRegularPlanList(cldRegularPlanDto);
        return miniProjectMapper.getRegularPlanList(cldRegularPlanDto);
    }

    @Override
    public List<CldRegularPlanVo> regularPlanList(CldRegularPlanDto cldRegularPlanDto) {
        return miniProjectMapper.regularPlanList(cldRegularPlanDto);
    }

    @Override
    public List<Map<String,Object>> getRegularPlanStaff(CldRegularPlanDto cldRegularPlanDto) {
        return miniProjectMapper.getRegularPlanStaff(cldRegularPlanDto);
    }

    @Override
    public CldRegularPlanVo getRegularPlanById(CldRegularPlanDto cldRegularPlanDto) {
        CldRegularPlanVo cldRegularPlanVo = miniProjectMapper.getRegularPlanById(cldRegularPlanDto);
        CldRegularPlanDetails query = new CldRegularPlanDetailsVo();
        query.setRegularPlanId(cldRegularPlanVo.getRegularPlanId());
        cldRegularPlanVo.setDetailsList(cldRegularPlanDetailsMapper.selectCldRegularPlanDetailsList(query));
        return cldRegularPlanVo;
    }

    @Override
    public List<CldRegularPlanVo> getRegularPlanStaffSubmit(CldRegularSubmit cldRegularSubmit) {
        return miniProjectMapper.getRegularPlanStaffSubmit(cldRegularSubmit);
    }
}
